package work.konbai.bmc.cms.controller;

import work.konbai.bmc.cms.domain.KindDO;
import work.konbai.bmc.cms.service.KindService;
import work.konbai.bmc.common.annotation.Log;
import work.konbai.bmc.common.config.Constant;
import work.konbai.bmc.common.controller.BaseController;
import work.konbai.bmc.common.domain.Tree;
import work.konbai.bmc.common.utils.R;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 物资分类管理
 *
 * @author KonBAI
 * @version 1.0
 * Create by 2020/02/07 22:12
 */
@Controller
@RequestMapping("/cms/kind")
public class KindController extends BaseController {

	private String prefix = "cms/kind";

	@Autowired
	private KindService kindService;

	/**
	 * 跳转物资分类管理
	 * @return
	 */
	@GetMapping()
	String kind() {
		return prefix + "/kind";
	}

	/**
	 * 获取物资分类列表
	 * @return
	 */
	@ApiOperation(value="获取物资分类列表", notes="")
	@ResponseBody
	@GetMapping("/list")
	public List<KindDO> list() {
		Map<String, Object> query = new HashMap<>(16);
		List<KindDO> kindList = kindService.list(query);
		return kindList;
	}

	/**
	 * 打开添加物资分类窗口
	 * @param pId
	 * @param model
	 * @return
	 */
	@ApiOperation(value="打开添加物资分类窗口", notes="")
	@GetMapping("/add/{pId}")
	String add(@PathVariable("pId") Long pId, Model model) {
		model.addAttribute("pId", pId);
		if (pId == 0) {
			model.addAttribute("pName", "总物资分类");
		} else {
			model.addAttribute("pName", kindService.get(pId).getName());
		}
		return  prefix + "/add";
	}

	/**
	 * 打开编辑物资分类窗口
	 * @param kindId
	 * @param model
	 * @return
	 */
	@ApiOperation(value="打开编辑物资分类窗口", notes="")
	@GetMapping("/edit/{kindId}")
	String edit(@PathVariable("kindId") Long kindId, Model model) {
		KindDO kind = kindService.get(kindId);
		model.addAttribute("kind", kind);
		if(Constant.KIND_ROOT_ID.equals(kind.getParentId())) {
			model.addAttribute("parentKindName", "无");
		}else {
			KindDO parKind = kindService.get(kind.getParentId());
			model.addAttribute("parentKindName", parKind.getName());
		}
		return  prefix + "/edit";
	}

	/**
	 * 保存物资分类列表
	 * @param kind 分类信息
	 * @return
	 */
	@Log("保存物资分类列表")
	@ApiOperation(value="保存物资分类列表", notes="")
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("cms:kind:add")
	public R save(KindDO kind) {
		if (kindService.save(kind) > 0) {
			return R.ok();
		}
		return R.error();
	}

	/**
	 * 修改物资分类列表
	 * @param kind 分类信息
	 * @return
	 */
	@Log("修改物资分类列表")
	@ApiOperation(value="修改物资分类列表", notes="")
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("cms:kind:edit")
	public R update(KindDO kind) {
		if (kindService.update(kind) > 0) {
			return R.ok();
		}
		return R.error();
	}

	/**
	 * 删除物资分类列表
	 * @param kindId
	 * @return
	 */
	@Log("删除物资分类列表")
	@ApiOperation(value="删除物资分类列表", notes="")
	@PostMapping("/remove")
	@ResponseBody
	@RequiresPermissions("cms:kind:remove")
	public R remove(Long kindId) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("parentId", kindId);
		if(kindService.count(map)>0) {
			return R.error(1, "包含下级物资分类,不允许修改");
		}
		if(kindService.checkKindHasUser(kindId)) {
			if (kindService.remove(kindId) > 0) {
				return R.ok();
			}
		}else {
			return R.error(1, "物资分类包含用户,不允许修改");
		}
		return R.error();
	}

	/**
	 * 批量删除物资分类列表
	 * @param kindIds
	 * @return
	 */
	@Log("批量删除物资分类列表")
	@ApiOperation(value="批量删除物资分类列表", notes="")
	@PostMapping("/batchRemove")
	@ResponseBody
	@RequiresPermissions("cms:kind:batchRemove")
	public R remove(@RequestParam("ids[]") Long[] kindIds) {
		kindService.batchRemove(kindIds);
		return R.ok();
	}

	/**
	 * 获取物资分类树信息
	 * @return
	 */
	@ApiOperation(value="获取物资分类树信息", notes="")
	@GetMapping("/tree")
	@ResponseBody
	public Tree<KindDO> tree() {
		Tree<KindDO> tree = new Tree<KindDO>();
		tree = kindService.getTree();
		return tree;
	}

	/**
	 * 跳转物资分类树窗口
	 * @return
	 */
	@ApiOperation(value="跳转物资分类树窗口", notes="")
	@GetMapping("/treeView")
	String treeView() {
		return  prefix + "/kindTree";
	}

}
